/*
 * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// NOLINTNEXTLINE(readability-function-size)
llvm::FunctionType *GetIntrinsicDeclaration(llvm::LLVMContext &ctx, ark::compiler::RuntimeInterface::IntrinsicId id)
{
    constexpr auto GC_SPACE = ark::llvmbackend::LLVMArkInterface::GC_ADDR_SPACE;
    switch (id) {
        case ark::compiler::RuntimeInterface::IntrinsicId::LIB_CALL_FMOD:
            return llvm::FunctionType::get(llvm::Type::getDoubleTy(ctx), {llvm::Type::getDoubleTy(ctx), llvm::Type::getDoubleTy(ctx)}, false);
        case ark::compiler::RuntimeInterface::IntrinsicId::LIB_CALL_FMODF:
            return llvm::FunctionType::get(llvm::Type::getFloatTy(ctx), {llvm::Type::getFloatTy(ctx), llvm::Type::getFloatTy(ctx)}, false);
        case ark::compiler::RuntimeInterface::IntrinsicId::LIB_CALL_LDEXP:
            return llvm::FunctionType::get(llvm::Type::getDoubleTy(ctx), {llvm::Type::getDoubleTy(ctx), llvm::Type::getInt32Ty(ctx)}, false);
        case ark::compiler::RuntimeInterface::IntrinsicId::LIB_CALL_LDEXPF:
            return llvm::FunctionType::get(llvm::Type::getFloatTy(ctx), {llvm::Type::getFloatTy(ctx), llvm::Type::getInt32Ty(ctx)}, false);
        case ark::compiler::RuntimeInterface::IntrinsicId::LIB_CALL_EXP2:
            return llvm::FunctionType::get(llvm::Type::getDoubleTy(ctx), {llvm::Type::getDoubleTy(ctx)}, false);
        case ark::compiler::RuntimeInterface::IntrinsicId::LIB_CALL_EXP2F:
            return llvm::FunctionType::get(llvm::Type::getFloatTy(ctx), {llvm::Type::getFloatTy(ctx)}, false);
        case ark::compiler::RuntimeInterface::IntrinsicId::LIB_CALL_MEM_COPY:
        case ark::compiler::RuntimeInterface::IntrinsicId::LIB_CALL_MEM_MOVE:
            return llvm::FunctionType::get(llvm::Type::getVoidTy(ctx),
                {llvm::PointerType::get(ctx, 0), llvm::PointerType::get(ctx, 0), llvm::Type::getInt64Ty(ctx)}, false);
        case ark::compiler::RuntimeInterface::IntrinsicId::LIB_CALL_MEM_SET:
            return llvm::FunctionType::get(llvm::Type::getVoidTy(ctx),
                {llvm::PointerType::get(ctx, 0), llvm::Type::getInt8Ty(ctx), llvm::Type::getInt64Ty(ctx)}, false);
        case ark::compiler::RuntimeInterface::IntrinsicId::LIB_CALL_MEMSET_8:
            return llvm::FunctionType::get(llvm::Type::getVoidTy(ctx),
                {llvm::PointerType::get(ctx, GC_SPACE), llvm::Type::getInt8Ty(ctx), llvm::Type::getInt32Ty(ctx), llvm::Type::getInt32Ty(ctx)}, false);
        case ark::compiler::RuntimeInterface::IntrinsicId::LIB_CALL_MEMSET_16:
            return llvm::FunctionType::get(llvm::Type::getVoidTy(ctx),
                {llvm::PointerType::get(ctx, GC_SPACE), llvm::Type::getInt16Ty(ctx), llvm::Type::getInt32Ty(ctx), llvm::Type::getInt32Ty(ctx)}, false);
        case ark::compiler::RuntimeInterface::IntrinsicId::LIB_CALL_MEMSET_32:
            return llvm::FunctionType::get(llvm::Type::getVoidTy(ctx),
                {llvm::PointerType::get(ctx, GC_SPACE), llvm::Type::getInt32Ty(ctx), llvm::Type::getInt32Ty(ctx), llvm::Type::getInt32Ty(ctx)}, false);
        case ark::compiler::RuntimeInterface::IntrinsicId::LIB_CALL_MEMSET_64:
            return llvm::FunctionType::get(llvm::Type::getVoidTy(ctx),
                {llvm::PointerType::get(ctx, GC_SPACE), llvm::Type::getInt64Ty(ctx), llvm::Type::getInt32Ty(ctx), llvm::Type::getInt32Ty(ctx)}, false);
        case ark::compiler::RuntimeInterface::IntrinsicId::LIB_CALL_MEMSET_F32:
            return llvm::FunctionType::get(llvm::Type::getVoidTy(ctx),
                {llvm::PointerType::get(ctx, GC_SPACE), llvm::Type::getFloatTy(ctx), llvm::Type::getInt32Ty(ctx), llvm::Type::getInt32Ty(ctx)}, false);
        case ark::compiler::RuntimeInterface::IntrinsicId::LIB_CALL_MEMSET_F64:
            return llvm::FunctionType::get(llvm::Type::getVoidTy(ctx),
                {llvm::PointerType::get(ctx, GC_SPACE), llvm::Type::getDoubleTy(ctx), llvm::Type::getInt32Ty(ctx), llvm::Type::getInt32Ty(ctx)}, false);
% Runtime::intrinsics.each do |intrinsic|
%   next unless intrinsic.respond_to?(:impl)
        case ark::compiler::RuntimeInterface::IntrinsicId::<%= intrinsic.enum_name %>:
            return llvm::FunctionType::get(<%= llvm_type_getter(intrinsic.impl_signature.ret, 'GC_SPACE') %>,
                       {<%= intrinsic.impl_signature.args.map { |t| llvm_type_getter(t, 'GC_SPACE') }.join(', ') %>}, false);
% end
        default:
            ASSERT_DO(false, (std::cerr << "Cannot get intrinsic signature by IntrinsicId = " << static_cast<int>(id)
                                        << std::endl));
            UNREACHABLE();
    }
}
